// pages/main/index.js

Page({
	data: {
		shopName: "",
		shopCount: 0,
		msg: "点我扫",
		note: "扫码购物",
		isShow: true,
		isError: false,
		urlAddress: getApp().globalData.imageUrl,
		right_distance: "-62rpx",
		left_name: "mymove1"

	},
	onLoad: function(options) {
		if (getApp().globalData.common.souce == "0") {
			this.openTheDoor();
		}
	},
	onReady: function() {

	},
	onShow: function() {
		// 页面显示
		var _self = this;

		if (getApp().globalData.common.shopCode) {
			_self.getShopMarket();
			_self.getShopName();
		} else {
			_self.setData({
				shopName: "请扫店铺二维码",
				isShow: false
			});
		}
	},
	onHide: function() {
		// 页面隐藏
	},
	onUnload: function() {
		// 页面关闭
	},
	getShopName: function() {
		// wx.showLoading({
		// 	title: '加载中',
		// })
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/shop/getShop.do',
			data: {
				shopCode: getApp().globalData.common.shopCode,
			},
			header: getApp().globalData.header,
			success: function(res) {
				if (res.data.code == 200) {
					_self.setData({
						shopName: res.data.obj.shopName
					});
				} else {
					_self.setData({
						shopName: '没有该门店'
					});
				}

			}
		})
	},
	addShopMarket: function(code, num) {
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/cart/addGoods.do',
			data: {
				gbk: code,
				shopCode: getApp().globalData.common.shopCode,
				num: num
			},
			header: getApp().globalData.header,
			success: function(res) {
				_self.setData({
					obj: res.data.obj,
					isShow: true
				});
				if (res.data.obj.lastGoodsisNull) {
					wx.showToast({
						title: '没有该商品',
						image: '../../image/error.png',
						duration: 1000
					})
				} else {
					_self.turn();
				}

			}
		})
	},
	getShopMarket: function() {
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/cart/getCartGoodsNum.do',
			data: {
				shopCode: getApp().globalData.common.shopCode,
			},
			header: getApp().globalData.header,
			success: function(res) {
				_self.setData({
					shopCount: res.data.obj,
					isShow: true,
					note: "扫码购物"
				});
				if (res.data.obj) {
					_self.setData({
						right_distance: "44rpx"
					});
				} else {
					_self.setData({
						right_distance: "-62rpx"
					});
				}
			}
		})
	},
	turn: function(argument) {
		if (this.data.isShow) {
			wx.showLoading({
				title: '加载中',
				mask: true
			});
			wx.navigateTo({
				url: '../scan/scan'
			})
		} else {
			this.scanShops();
		}

	},
	chickAction: function() {
		if (this.data.isShow) {
			this.scanGoods();
		} else {
			this.scanShops();
		}
	},
	scanShops: function() {
		var _self = this;
		wx.showModal({
			title: '提示',
			content: '请先扫描店铺二维码',
			showCancel: true,
			success: function(res) {
				if (res.confirm) {
					wx.scanCode({
						success: (res) => {
							if (!res.path) {
								wx.showToast({
									title: '此码非小程序码',
									image: '../../image/error.png',
									duration: 1000
								})
							} else {
								var shopCode = "";
								var type = "";
								shopCode = res.path.split("?")[1];
								type = shopCode.split("&")[1];
								shopCode = shopCode.split("&")[0];
								shopCode = shopCode.split("=")[1];
								type = type.split("=")[1];
								getApp().globalData.common.shopCode = shopCode;
								getApp().globalData.common.souce = type;
								_self.getShopMarket();
								_self.getShopName();
								wx.setStorageSync('shopCode', shopCode);

								if (type == '0') {
									_self.openTheDoor();
								}

							}
						}
					})
				}
			}
		})
	},
	scanGoods: function() {
		var _self = this;
		wx.scanCode({
			success: (res) => {
				var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]")
				console.log(pattern.test(res.result))
				if (!pattern.test(res.result)) {
					_self.addShopMarket(res.result, 1);
				} else {
					_self.showMyToast(_self, "非商品条码")
					// wx.showToast({
					// 	title: '非商品条码',
					// 	image: '../../image/error.png',
					// 	duration: 2000
					// })
				}


			}
		})
	},
	openTheDoor: function() {
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/shop/scanRqOpenDoor.do',
			data: {
				shopCode: getApp().globalData.common.shopCode,
			},
			header: getApp().globalData.header,
			success: function(res) {
				getApp().globalData.common.souce = "1"
				if (res.data.code == 200) {
					wx.showToast({
						title: '开门成功',
						icon: 'success',
						duration: 2000
					})
				} else {
					// wx.showToast({
					// 	title: '开门失败',
					// 	image: '../../image/error.png',
					// 	duration: 2000
					// })
				}

			},
			fail: function() {
				// wx.showToast({
				// 	title: '开门失败',
				// 	image: '../../image/error.png',
				// 	duration: 2000
				// })
			}
		})

	},
	showMyToast: function(self, my_toast_tip, time = 1000, template = 'my_toast') {
		self.setData({
			template: template, //my_toast
			my_toast_tip: my_toast_tip
		})
		var timer = setTimeout(() => {
			self.setData({
				template: '', //my_toast
				my_toast_tip: ''
			})
			clearTimeout(timer);
		}, time)

	}

})